<?php
/**
 * Copyright © 2013-2017 Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

// @codingStandardsIgnoreFile

?>
<?php /** @var $block Magento\Shipping\Block\Adminhtml\Order\Tracking\View */ ?>
<div class="admin__control-table-wrapper">
    <table class="data-table admin__control-table" id="shipment_tracking_info">
        <thead>
            <tr class="headings">
                <th class="col-carrier"><?php /* @escapeNotVerified */ echo __('Carrier') ?></th>
                <th class="col-title"><?php /* @escapeNotVerified */ echo __('Title') ?></th>
                <th class="col-number"><?php /* @escapeNotVerified */ echo __('Number') ?></th>
                <th class="col-delete last"><?php /* @escapeNotVerified */ echo __('Action') ?></th>
            </tr>
        </thead>
        <tfoot>
            <tr>
                <td class="col-carrier">
                    <select name="carrier"
                            class="select admin__control-select"
                            onchange="selectCarrier(this)">
                        <?php foreach ($block->getCarriers() as $_code => $_name): ?>
                        <option value="<?php /* @escapeNotVerified */ echo $_code ?>"><?php echo $block->escapeHtml($_name) ?></option>
                        <?php endforeach; ?>
                    </select>
                </td>
                <td class="col-title">
                    <input class="input-text admin__control-text"
                           type="text"
                           id="tracking_title"
                           name="title"
                           value="" />
                </td>
                <td class="col-number">
                    <input class="input-text admin__control-text"
                           type="text"
                           id="tracking_number"
                           name="number"
                           value="" />
                </td>
                <td class="col-delete last"><?php echo $block->getSaveButtonHtml() ?></td>
            </tr>
        </tfoot>
    <?php if ($_tracks = $block->getShipment()->getAllTracks()): ?>
        <tbody>
        <?php $i = 0; foreach ($_tracks as $_track):$i++ ?>
            <tr class="<?php /* @escapeNotVerified */ echo($i%2 == 0) ? 'even' : 'odd' ?>">
                <td class="col-carrier"><?php echo $block->escapeHtml($block->getCarrierTitle($_track->getCarrierCode())) ?></td>
                <td class="col-title"><?php echo $block->escapeHtml($_track->getTitle()) ?></td>
                <td class="col-number">
                    <?php if ($_track->isCustom()): ?>
                    <?php echo $block->escapeHtml($_track->getNumber()) ?>
                    <?php else: ?>
                    <a href="#" onclick="popWin('<?php /* @escapeNotVerified */ echo $this->helper('Magento\Shipping\Helper\Data')->getTrackingPopupUrlBySalesModel($_track) ?>','trackorder','width=800,height=600,resizable=yes,scrollbars=yes')"><?php echo $block->escapeHtml($_track->getNumber()) ?></a>
                    <div id="shipment_tracking_info_response_<?php /* @escapeNotVerified */ echo $_track->getId() ?>"></div>
                    <?php endif; ?>
                </td>
                <td class="col-delete last"><button class="action-delete" type="button" onclick="deleteTrackingNumber('<?php /* @escapeNotVerified */ echo $block->getRemoveUrl($_track) ?>'); return false;"><span><?php /* @escapeNotVerified */ echo __('Delete') ?></span></button></td>
            </tr>
        <?php endforeach; ?>
        </tbody>
    <?php endif; ?>
    </table>
</div>

<script>
require(['prototype'], function(){

//<![CDATA[
function selectCarrier(elem) {
    var option = elem.options[elem.selectedIndex];
    $('tracking_title').value = option.value && option.value != 'custom' ? option.text : '';
}

function deleteTrackingNumber(url) {
    if (confirm('<?php /* @escapeNotVerified */ echo __('Are you sure?') ?>')) {
        submitAndReloadArea($('shipment_tracking_info').parentNode, url)
    }
}

window.selectCarrier = selectCarrier;
window.deleteTrackingNumber = deleteTrackingNumber;
//]]>

});
</script>
